package com.sailmi.auth.userprovider.gatp;
import com.sailmi.auth.userprovider.gatp.feign.IUserClient;
import com.sailmi.common.model.auth.AuthUser;
import com.sailmi.common.secure.user.IUserProvider;
import com.sailmi.common.tool.api.R;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

@Component
public class UserProvider implements IUserProvider {
	private static final Logger log = LoggerFactory.getLogger(UserProvider.class);
    @Resource
	IUserClient userClient;

	@Override
	public AuthUser userInfo(String userAccount) {
		return null;
	}

	@Override
	public AuthUser registerUser(AuthUser authUser) {
		return null;
	}

	@Override
	public AuthUser updateUser(AuthUser authUser) {
		return null;
	}

	public AuthUser userInfo(Long userId) {
        log.debug("Now get user info "+userId );
        R<AuthUser> auth=userClient.userInfo(userId);
		AuthUser userInfo = auth.getData();
		log.debug("Yes Get User from remote:"+userInfo);
        return userInfo;
    }

    public AuthUser userInfo(String tenantId, String account, String password) {
        log.info("Now in UserProvider for sailmi................................."+account);
        R<AuthUser> auth=userClient.userInfo(account, password);
		log.info("Now in UserProvider for sailmi................................."+auth);
		AuthUser userInfo = auth.getData();
		log.info("Now in UserProvider for sailmi................................."+userInfo);
        if(userInfo!=null){
            //如果权限为空，则调用权限接口为用户初始化权限
            if(userInfo!=null&&userInfo.getRoles()==null){
            	try {
					R<List> roles = userClient.getRoles(String.valueOf(userInfo.getUserId()));
					System.out.println("Result Json:" + roles);
					List roleList = roles.getData();
					if (roleList != null) {
						userInfo.setRoles(roleList);
					}
					R<List> rights = userClient.getRights(String.valueOf(userInfo.getUserId()));
					if(Objects.nonNull(rights)){
						List rightList = rights.getData();
						if (rightList != null) {
							userInfo.setPermissions(rightList);
						}
					}
				}catch(Exception ec){
            		ec.printStackTrace();
				}
            }
            return userInfo;
        }
        return null;
    }

    public Long submitUserInfo(AuthUser user) {
        return null;
    }

    public Boolean resetUserPass(String id) {
        return null;
    }
}
